package com.ruoyi.source.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourcePushResultInfoMapper;
import com.ruoyi.db.domain.SourcePushResultInfo;
import com.ruoyi.source.service.ISourcePushResultInfoService;

/**
 * 线索推送结果Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourcePushResultInfoServiceImpl implements ISourcePushResultInfoService {
    @Autowired
    private SourcePushResultInfoMapper sourcePushResultInfoMapper;

    /**
     * 查询线索推送结果
     *
     * @param id 线索推送结果主键
     * @return 线索推送结果
     */
    @Override
    public SourcePushResultInfo selectSourcePushResultInfoById(Long id) {
        return sourcePushResultInfoMapper.selectSourcePushResultInfoById(id);
    }

    @Override
    public List<Map<String, Object>> getcost(Integer type) {
        LocalDate dateInfo = LocalDate.now();
        List<Map<String, Object>> mapList = new ArrayList<>();

        if (type == 0 || type == 1) {
            dateInfo = (type == 0) ? dateInfo : dateInfo.minusDays(1); // 今天或者昨天的日期
            mapList = sourcePushResultInfoMapper.getcost(dateInfo.toString());
        } else if (type == 2 || type == 3) {
            int date = (type == 2) ? 3 : 5; // 根据类型确定要减去的天数
            LocalDate startDate = dateInfo.minusDays(date); // 开始日期
            LocalDate endDate = LocalDate.now().minusDays(1); // 结束日期
            mapList = sourcePushResultInfoMapper.getcostBetween(startDate.toString(), endDate.toString());
        }
        // 确保 mapList 不为 null
        if (mapList == null) {
            mapList = new ArrayList<>();
        }
        return mapList;
    }
    @Override
    public List<Map<String, Object>> getCpsCost(SourcePushResultInfo sourcePushResultInfo) {

        return sourcePushResultInfoMapper.getCpsCost(sourcePushResultInfo);
    }


    /**
     * 查询线索推送结果列表
     *
     * @param sourcePushResultInfo 线索推送结果
     * @return 线索推送结果
     */
    @Override
    public List<SourcePushResultInfo> selectSourcePushResultInfoList(SourcePushResultInfo sourcePushResultInfo) {
        return sourcePushResultInfoMapper.selectSourcePushResultInfoList(sourcePushResultInfo);
    }

    /**
     * 查询线索推送结果列表
     *
     * @param sourcePushResultInfo 线索推送结果
     * @return 线索推送结果
     */
    @Override
    public List<Map<String, Object>> selectSourcePushResultInfoMapList(SourcePushResultInfo sourcePushResultInfo) {
        return sourcePushResultInfoMapper.selectSourcePushResultInfoMapList(sourcePushResultInfo);
    }

    @Override
    public List<Map<String, Object>> selectSourcePushResultInfoMapList1(SourcePushResultInfo sourcePushResultInfo) {
        return sourcePushResultInfoMapper.selectSourcePushResultInfoMapList1(sourcePushResultInfo);
    }

    @Override
    public List<Map<String, Object>> selectAccountMapList(SourcePushResultInfo sourcePushResultInfo) {
        return sourcePushResultInfoMapper.selectAccountMapList(sourcePushResultInfo);
    }

    /**
     * 新增线索推送结果
     *
     * @param sourcePushResultInfo 线索推送结果
     * @return 结果
     */
    @Override
    public int insertSourcePushResultInfo(SourcePushResultInfo sourcePushResultInfo) {
        sourcePushResultInfo.setCreateTime(DateUtils.getNowDate());
        return sourcePushResultInfoMapper.insertSourcePushResultInfo(sourcePushResultInfo);
    }

    /**
     * 修改线索推送结果
     *
     * @param sourcePushResultInfo 线索推送结果
     * @return 结果
     */
    @Override
    public int updateSourcePushResultInfo(SourcePushResultInfo sourcePushResultInfo) {
        sourcePushResultInfo.setUpdateTime(DateUtils.getNowDate());
        return sourcePushResultInfoMapper.updateSourcePushResultInfo(sourcePushResultInfo);
    }

    /**
     * 批量删除线索推送结果
     *
     * @param ids 需要删除的线索推送结果主键
     * @return 结果
     */
    @Override
    public int deleteSourcePushResultInfoByIds(Long[] ids) {
        return sourcePushResultInfoMapper.deleteSourcePushResultInfoByIds(ids);
    }

    /**
     * 删除线索推送结果信息
     *
     * @param id 线索推送结果主键
     * @return 结果
     */
    @Override
    public int deleteSourcePushResultInfoById(Long id) {
        return sourcePushResultInfoMapper.deleteSourcePushResultInfoById(id);
    }

    @Override
    public String getTotal(SourcePushResultInfo sourcePushResultInfo) {
        return sourcePushResultInfoMapper.getTotal(sourcePushResultInfo);
    }
}
